<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/07/16 13:53:20 $)
// $RCSfile: default.login.php,v $ : $Revision: 1.6 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Initialization function. You can connect to your database etc over here.
*/
function loginShareInit()
{
	global $loginshare;

	$loginshare->moduleloaded = true;
}

/**
* Authorize a user based on email and password
*/
function loginShareAuthorize($email, $password)
{
	global $dbCore, $_SWIFT, $cookie;

	// Get the user id associated with this email
	$_email = $dbCore->queryFetch("SELECT `userid` FROM `". TABLE_PREFIX ."useremails` WHERE `email` = '". $dbCore->escape($email) ."';");
	if (empty($_email["userid"]))
	{
		return false;
	}

	$_user = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."users` WHERE `userid` = '". intval($_email["userid"]) ."';");

	$loginmod = false;
/*	if (empty($_user["loginapi_moduleid"]) || $_user["loginapi_moduleid"] == LOGINAPI_DEFAULT)
	{
		$loginmod = true;
	}*/

	$_cookieset = $cookie->getSCookie("loginemail");
	if(!empty($_cookieset))
	{
		$password_chk = $password;
	}else{
		$password_chk = md5($password);
	}

	if ($_user["userpassword"] == $password_chk && $_user["enabled"] == 1 && $loginmod = true)
	{
		// Authenticated
		$_SWIFT["user"] = $_user;

		return $_user["userid"];
	} else {
		unset($_SWIFT["user"]);

		return false;
	}
}

/**
* Return the Unique User ID of the current user
*/
function loginShareUserID()
{
	global $_SWIFT;

	if (empty($_SWIFT["user"]["userid"]))
	{
		return false;
	} else {
		return $_SWIFT["user"]["userid"];
	}
}

/**
* Logout the current user
*/
function loginShareLogout()
{
	global $session, $_SWIFT;

	$session->updateSession($_SWIFT["session"]["sessionid"], 0);

	return true;
}

/**
* Load the user credentials into current workspace. The following variables should be declared for proper working:
* userid - User id that is set in the "users" table
* fullname
* email - Array
* password (MD5 Hashed)
* usergroupid - If this is not set, then it will use the default registered user group for this template group
*/
function loginShareLoadUser()
{
	global $dbCore, $_SWIFT, $loginshare;

	if (empty($_SWIFT["session"]["typeid"]))
	{
		$_SWIFT["user"]["loggedin"] = false;
		return false;
	}

	$_user = $loginshare->loadSWIFTUser($_SWIFT["session"]["typeid"]);
	if (!$_user)
	{
		$_SWIFT["user"]["loggedin"] = false;

		return false;
	}

	$_SWIFT["user"] = $_user;

	return true;
}

/**
* Render Login Share
*/
function renderLoginShareForm()
{
	return array();
}
?>